<template>
  <div class="mobile" :hidden="seen">
    <a-card class="login-bg"></a-card>
    <div class="login-main">
      <div class="login-head">
        <img class="login-logo" src="../../assets/img/mobile/logo.png">
        <p>唐山未检</p>
      </div>
      <a-card class="login-box">
        <a-tabs default-active-key="1" @change="callback">
          <a-tab-pane key="1" tab="登录">
            <a-form-model>
              <a-form-model-item>
                <a-input placeholder="请输入学号" v-model="model.uName">
                  <i slot="prefix" class="iconfont icondenglu-xuehao"></i>
                </a-input>
              </a-form-model-item>
              <a-form-model-item>
                <a-input-password type="password" placeholder="请输入密码" v-model="model.passwd">
                  <i slot="prefix" class="iconfont iconmimacopy"></i>
                </a-input-password>
              </a-form-model-item>
              <a-form-model-item>
                <a-button type="primary" html-type="submit" @click="mobileLogin">立即登录</a-button>
              </a-form-model-item>
              <p class="tips"><i class="iconfont icontiaokuanshengming"></i>初次登录请先注册，并设置登录密码</p>
            </a-form-model>
            <a-button type="link" @click="forgetPwd()" v-show="showBtn">忘记密码？</a-button>
          </a-tab-pane>
          <a-tab-pane key="2" tab="注册">
            <a-form-model>
              <a-form-model-item>
                <a-input placeholder="姓名" v-model="model.realName">
                  <i slot="prefix" class="iconfont iconzhanghao"></i>
                </a-input>
              </a-form-model-item>
              <a-form-model-item>
                <a-input placeholder="学号" v-model="model.userName">
                  <i slot="prefix" class="iconfont icondenglu-xuehao"></i>
                </a-input>
              </a-form-model-item>
              <a-form-model-item>
                <a-input-password type="password" placeholder="设置密码" v-model="model.password">
                  <i slot="prefix" class="iconfont iconmimacopy"></i>
                </a-input-password>
              </a-form-model-item>
              <a-form-model-item>
                <a-input-password type="password" placeholder="确认密码" v-model="model.pwd">
                  <i slot="prefix" class="iconfont iconkey-fill"></i>
                </a-input-password>
              </a-form-model-item>
              <a-form-model-item>
                <a-button type="primary" html-type="submit" @click="registerUser">立即注册</a-button>
              </a-form-model-item>
              <p class="tips"><i class="iconfont icontiaokuanshengming"></i>请注意学生姓名填写，学号为学生在校学号，密码区分大小写</p>
            </a-form-model>
          </a-tab-pane>
        </a-tabs>
      </a-card>
    </div>


  </div>

</template>

<script>
  /*const APPID='wx6ae10c00bb5aeb40';*/
  const APPID=window.vueConfig.appId;
  const currentUrl = window.location.href;

  import { postAction,httpAction } from '@/api/manage'
  import https from '@/api/http-service'

  export default {
    components: {},
    data () {
      return {
        model:{},
        menuFlag:"",
        openId:"",
        seen:true,
        newRegister:"no",
        clientHeight:document.documentElement.clientHeight,
        showBtn: true,  // 控制按钮盒子显示隐藏
      }
    },
    created () {
      this.getWxCode();
    },
    methods: {
      getWxCode () {
        const code = https.getUrlParam('code'); // 截取路径中的code，如果没有就去微信授权，如果已经获取到了就直接传code给后台获取openId
        if (code ==null || code ==='') {
          window.location.href ='https://open.weixin.qq.com/connect/oauth2/authorize?appid=' + APPID +'&redirect_uri=' +encodeURIComponent(currentUrl) +'&response_type=code&scope=snsapi_base&state=1#wechat_redirect'
        }else {
          this.getOpenId(code);
          localStorage.setItem("wxCode", code);  //取到code值
        }
      },
      getOpenId(code){
        let that = this;
        let username = localStorage.getItem("username");
        let password = localStorage.getItem("password");
        postAction("/mobile/getOpenId?code="+code+"&username="+username+"&password="+password).then(res=>{
          if(res.success){
            that.openId = res.result.openid
            localStorage.setItem("openId", res.result.openid);
            localStorage.setItem("accessToken", res.result.access_token);
            let loginFlag = localStorage.getItem("loginFlag");
            const menuFlag = https.getUrlParam('flag');
            this.menuFlag = menuFlag;
            if(res.result.userExist == "yes" && loginFlag == "suc"){
              if(menuFlag == "hrhs"){
                this.getGoodThingsList();
                return false;
              }else if(menuFlag == "wcnrbh"){
                this.getWcnrbh();
                return false;
              }else if(menuFlag == "grxx"){
                this.getGrxx();
                return false;
              }
            }else{
              if(res.result.userExist != "" && res.result.userExist != null){
                this.$message.error(res.result.userExist);
              }
              that.seen = false;
            }
          }
        }).catch(()=>{

        })
      },
      getGoodThingsList(){
        let userId = localStorage.getItem("userId");
        this.$router.push({path:'/GoodThingsList',query:{userId:userId}});
      },
      getWcnrbh(){
        let userId = localStorage.getItem("userId");
        this.$router.push({path:'/MinorsProtecteGuide',query:{userId:userId}});
      },
      getGrxx(){
        let userId = localStorage.getItem("userId");
        this.$router.push({path:'/PersonalMsg',query:{userId:userId}});
      },
      mobileLogin(){
        const that = this;
        let loginParams = {};
        let openId = localStorage.getItem("openId");
        let uName = this.model.uName;
        let passwd = this.model.passwd;
        if(uName == "" || typeof(uName) == "undefined"){
          this.$message.error("请输入学号！");
          return false;
        }
        if(passwd == "" || typeof(passwd) == "undefined"){
          this.$message.error("请输入密码！");
          return false;
        }
        loginParams.openId = openId;
        loginParams.uName = uName;
        loginParams.passwd = passwd;
        postAction("/mobile/login?openId="+openId+"&uName="+uName+"&passwd="+passwd).then(res=>{
          if(res.success){
            localStorage.setItem("userId", res.result.userId);
            localStorage.setItem("username", uName);
            localStorage.setItem("password", passwd);
            localStorage.setItem("loginFlag","suc");
            if(this.menuFlag == "hrhs"){
              that.getGoodThingsList();
            }else if(this.menuFlag == "wcnrbh"){
              that.getWcnrbh();
            }else if(this.menuFlag == "grxx"){
              that.getGrxx();
            }
          }else{
            this.$message.error(res.message);
          }
        })
      },
      registerUser(){
        const that = this;
        let registerParams = {};
        let realname = this.model.realName;
        let username = this.model.userName;
        let password = this.model.password;
        let pwd = this.model.pwd;
        let openId = localStorage.getItem("openId");
        if(realname == "" || typeof(realname) == "undefined"){
          this.$message.error("姓名不能为空！");
          return false;
        }
        if(username == "" || typeof(username) == "undefined"){
          this.$message.error("学号不能为空！");
          return false;
        }
        if(password == "" || typeof(password) == "undefined"){
          this.$message.error("设置密码不能为空！");
          return false;
        }
        if(pwd == "" || typeof(pwd) == "undefined"){
          this.$message.error("确认密码不能为空！");
          return false;
        }
        if(password != pwd && typeof(password) != "undefined" && typeof(pwd) != "undefined"){
          this.$message.error("设置密码和确认密码需一致！");
          return false;
        }
        registerParams.realname = realname;
        registerParams.username = username;
        registerParams.password = password;
        registerParams.openId = openId;
        postAction("/mobile/registerUser?realname="+realname+"&username="+username+"&password="+password+"&openId="+openId+"&newRegister="+this.newRegister,registerParams).then(res=>{
          if(res.success){
            this.model.realName = "";
            this.model.userName = "";
            this.model.password = "";
            this.model.pwd = "";
            this.newRegister = "no";
            this.$message.success("注册成功！");
          }else{
            this.$message.error(res.message);
          }
        })
      },
      forgetPwd(){
       let that = this;
        this.$confirm({
          title:"忘记密码",
          content:"是否重新注册?",
          cancelText: "取消",
          okText: "确定",
          onOk: () => {
            that.$message.success("与该微信绑定的学生信息重置成功,请到注册页面重新注册！");
            that.newRegister = "yes";
          }
        });
      }
    },

    mounted() {
      window.onresize= ()=>{
        if(this.clientHeight>document.documentElement.clientHeight) {
          this.showBtn =false
        }else{
          this.showBtn = true
        }
      }

    }
  }
</script>
<style lang="less" scoped>

</style>